﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data.SqlClient;
using IFRS.DataAccess.DAL;
using System.Collections;
using System.Configuration;
using IFRS.Models;
using System.Data;

public partial class PopupPages_GLAccount_GL : PopupBase
{

    private GLAccount GLAccount = new GLAccount();
    private Paging paging = new Paging();

    private string SortingExpression
    {
        get
        {
            return ViewState["SortingExpression"] != null
                ? Convert.ToString(ViewState["SortingExpression"])
                : string.Empty;
        }
        set { ViewState["SortingExpression"] = value; }
    }
    private SortDirection? SortingDirection
    {
        get
        {
            return ViewState["SortDirection"] != null
                ? (SortDirection?)ViewState["SortDirection"]
                : null;
        }
        set { ViewState["SortDirection"] = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.PopupTitle = Resources.ifrsMenu.SearchGLAccount; 
            this.SetControlAttribute();
        }
    }

    protected void radCheck_CheckedChanged(object sender, EventArgs e)
    {
        this.SaveData();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindSearchData();
    }

    protected void Page_Changed(object sender, EventArgs e)
    {
        int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
        this.GetCustomersPageWise(pageIndex);
    }

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.SetRowAttribute("GLAccountID");
            e.Row.SetRowAttribute("GLAccountCode");
            e.Row.SetRowAttribute("GLAccountName");
        }
    }
    protected void gvSearch_Sorting(object sender, GridViewSortEventArgs e)
    {
        paging = Page.LoadViewState(paging, ViewState);

        SortingExpression = e.SortExpression.ToUpper();
        SortingDirection = SortingDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;

        paging.Sorting = e.Sorting(SortingExpression, SortingDirection);
        Page.SaveViewState(paging, ViewState);

        GetCustomersPageWise(paging.PageIndex);
    }

    private void PopulatePager(int recordCount, int currentPage)
    {
        // double dblPageCount = (double)((decimal)recordCount / decimal.Parse(ddlPageSize.SelectedValue));
        double dblPageCount = (double)((decimal)recordCount / 10);
        int pageCount = (int)Math.Ceiling(dblPageCount);

        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            pages.Add(new ListItem("First", "1", currentPage > 1));
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }

            pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
        }

        repPager.DataSource = pages;
        repPager.DataBind();
    }

    private void GetCustomersPageWise(int pageIndex)
    {

        paging = Page.LoadViewState(paging, ViewState) ?? new Paging();
        paging.Sorting = paging.Sorting == null ? "" : paging.Sorting;
        paging.PageSize = gvSearch.PageSize;
        paging.PageIndex = pageIndex;

        GLAccount.GLAccountCode = txtSearch1.Text.Trim();
        GLAccount.GLAccountName = txtSearch2.Text.Trim();

        int recordCount = 0;
        var spr = new GL_ACCOUNT();
        var dt = spr.SEARCH_ALL(GLAccount, paging, ref recordCount);

        gvSearch.DataSource = dt;
        gvSearch.DataBind();
        this.PopulatePager(recordCount, pageIndex);

        Page.SaveViewState(paging, ViewState);
    }

    private void BindSearchData()
    {
        this.GetCustomersPageWise(1);
    }

    private void SaveData()
    {
        string sText = "";

        for (int i = 0; i < gvSearch.Rows.Count; i++)
        {
            RadioButton chk = (RadioButton)gvSearch.Rows[i].Cells[0].FindControl("radCheck");

            if (chk != null)
            {
                if (chk.Checked)
                {
                    GridViewRow gvRow = gvSearch.Rows[i];
                    sText = gvRow.Attributes["GLAccountID"] + "$" + gvRow.Attributes["GLAccountCode"] + "$" + gvRow.Attributes["GLAccountName"];
                    break;
                }
            }
        }

        if (sText != "")
        {
            string[] strSplitArr = sText.Split('$');

            string sScript = string.Format("<script type='text/javascript'>MainValues('{0}','{1}','{2}','{3}');</script>", strSplitArr[0], strSplitArr[1], strSplitArr[2],Request.QueryString["PopUp"].ToString());
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "MainValues", sScript, false);
        }


    }

    private void SetControlAttribute()
    {
        btnCancel.Attributes.Add("onclick", "CloseForm();");
    }
}